package com.zhdl.network.other_protocol.modbustcp.analysis;

import com.project.command.faq.state.LinkStateService;
import lombok.Getter;
import lombok.Setter;
import org.tio.client.intf.TioClientListener;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;

/**
 * ModbusTCP事件
 */
public class ModbusTcpListener implements TioClientListener {

    private LinkStateService linkStateService;

    @Getter
    @Setter
    private String server;

    public void setStateService(LinkStateService linkStateService) {
        this.linkStateService = linkStateService;
    }

    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
        linkStateService.updatePortState(server, isConnected);
    }

    @Override
    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
    }

    @Override
    public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
    }

    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
    }

    @Override
    public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
    }

    @Override
    public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception {

        linkStateService.updatePortState(server, false);
    }
}
